package com.gitee.controller;

import com.gitee.bo.AdminBo;
import com.gitee.bo.AdminRoleBo;
import com.gitee.enums.ResponseEnum;
import com.gitee.pojo.Role;
import com.gitee.service.IAdminRoleService;
import com.gitee.service.IAdminService;
import com.gitee.service.IRoleService;
import com.gitee.vo.AdminVo;
import com.gitee.vo.PageVo;
import com.gitee.vo.ResponseVo;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 管理员管理
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IAdminRoleService adminRoleService;

    @Autowired
    private IRoleService roleService;

    /**
     * 管理员列表
     * @param keywords
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseVo<PageVo<AdminVo>> list(String keywords, Integer pageNum, Integer pageSize) {
        PageVo<AdminVo> adminVoPageVo = adminService.pageAdmin(keywords, pageNum, pageSize);
        return ResponseVo.ok(adminVoPageVo);
    }

    /**
     * 添加管理员
     * @param adminBo
     * @return
     */
    @PostMapping
    public ResponseVo<String> add(@Valid @RequestBody AdminBo adminBo) {
        adminService.addAdmin(adminBo);
        return ResponseVo.ok(ResponseEnum.ADD_SUCCESS);
    }

    /**
     * 更新管理员
     * @param adminBo
     * @param adminId
     * @return
     */
    @PutMapping("/{adminId}")
    public ResponseVo<String> update(@Valid @RequestBody AdminBo adminBo, @PathVariable Integer adminId) {
        adminService.updateAdmin(adminBo, adminId);
        return ResponseVo.ok(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 删除用户
     * @param adminId
     * @return
     */
    @DeleteMapping("/{adminId}")
    public ResponseVo<String> delete(@PathVariable Integer adminId) {
        adminService.deleteAdmin(adminId);
        return ResponseVo.ok(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 批量删除用户
     * @param adminIds
     * @return
     */
    @DeleteMapping("/batch")
    public ResponseVo<String> deleteBatch(@RequestBody Integer[] adminIds) {
        adminService.deleteAdminBatch(adminIds);
        return ResponseVo.ok(ResponseEnum.DELETE_SUCCESS);
    }

    /**
     * 根据用户id更新权限
     * @param adminRoleBo
     * @param adminId
     * @return
     */
    @PutMapping("/permission/{adminId}")
    public ResponseVo<String> updatePermissionByAdminId(@RequestBody AdminRoleBo adminRoleBo, @PathVariable Integer adminId) {
        adminRoleService.updateByAdminId(adminId, adminRoleBo);
        return ResponseVo.ok(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
     * 角色列表
     * @return
     */
    @GetMapping("/role")
    public ResponseVo<List<Role>> list() {
        List<Role> roles = roleService.list();
        return ResponseVo.ok(roles);
    }
}
